package sy.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sy.jackson.SysProjectMixin;
import sy.jackson.SysUnitMixin;
import sy.util.MessageObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.model.WYAccount;
import sy.model.WYRole;
import sy.model.WYUser;
import sy.service.WYAccountServiceI;
import sy.service.WYRoleServiceI;
import sy.service.WYUserServiceI;
/**
 * @Description: 首页
 * @param 
 * @author     :wjy
 * Create Date : 2017年7月26日 
 */
@Controller
@RequestMapping("wyUserController")
public class WYUserController {
	@Autowired
	private WYUserServiceI wyUserService;
	@Autowired
	private WYAccountServiceI wyAccountService;
	private static final ObjectMapper objectMapper = new ObjectMapper();

	/************************** view controller ***************************/

	/**
	 * 
	 * @Title: objectView
	 * @Description: TODO
	 * @param: @param model
	 * @param: @return     
	 * @return: String     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月6日 下午4:43:06
	 * @throws
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
		List<WYUser> wyUserList = wyUserService.listWYUser();
		model.addAttribute("wyUserList",wyUserList);
		return "userInfo/wyUser/wyUser";
	}
	
	/**
	 * 
	 * @Title: newObjectInfo
	 * @Description: TODO
	 * @param: @param model
	 * @param: @return     
	 * @return: String     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月6日 下午4:44:12
	 * @throws
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		String uuid = UUID.randomUUID().toString();
		WYUser wyUser = new WYUser();
		wyUser.setId(uuid);
		List<WYAccount> wyAccountList = wyAccountService.listWYAccount();
		model.addAttribute("wyAccountList", wyAccountList);
		model.addAttribute("wyUser",wyUser);
		model.addAttribute("type",1);
		
		return "userInfo/wyUser/wyUserFrom";
	}

	/**
	 * 
	 * @Title: editObjectInfo
	 * @Description: TODO
	 * @param: @param id
	 * @param: @param model
	 * @param: @return     
	 * @return: String     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月7日 下午8:03:31
	 * @throws
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam String id,Model model){
		
		WYUser wyUser = wyUserService.getWYUserById(id);
		List<WYAccount> wyAccountList = wyAccountService.listWYAccount();
		model.addAttribute("wyAccountList", wyAccountList);
		model.addAttribute("wyUser",wyUser);
		model.addAttribute("type",2);
		
		return "userInfo/wyUser/wyUserFrom";
	}
		
	/************************** data controller ***************************/
	
	/**
	 * 
	 * @Title: newObjectInfo
	 * @Description: TODO
	 * @param: @param wyUser
	 * @param: @param model
	 * @param: @return     
	 * @return: MessageObject     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月7日 下午8:03:36
	 * @throws
	 */
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	@ResponseBody
	private MessageObject newObjectInfo( @RequestBody WYUser wyUser, Model model) {
		MessageObject messageObject = new MessageObject();
		try {
			wyUserService.save(wyUser);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * 
	 * @Title: editObjectInfo
	 * @Description: TODO
	 * @param: @param wyUser
	 * @param: @param model
	 * @param: @return     
	 * @return: MessageObject     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月7日 下午8:03:40
	 * @throws
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	private MessageObject editObjectInfo( @RequestBody WYUser wyUser, Model model) {
		MessageObject messageObject = new MessageObject();
		try {
			wyUserService.modify(wyUser);
			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * 
	 * @Title: delObjectInfo
	 * @Description: TODO
	 * @param: @param ids
	 * @param: @return     
	 * @return: MessageObject     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月7日 下午8:03:45
	 * @throws
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") List<String> ids) {
		MessageObject messageObject = new MessageObject();
		try {
			wyUserService.deleteByIds(ids);
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
	
	/**
	 * 
	 * @Title: getAllData
	 * @Description: TODO
	 * @param: @param response
	 * @param: @return     
	 * @return: String     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月7日 下午8:03:53
	 * @throws
	 */
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	@ResponseBody
	private String getAllData(HttpServletResponse response) {

		List<WYUser> wyUserList = wyUserService.listWYUser();
		try {
			return objectMapper.writeValueAsString(wyUserList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
}
